package com.example.jbmsuppliers.sinopec.orderaddress;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.jbmsuppliers.R;
import com.example.jbmsuppliers.sinopec.activity.ActivityUse;
import com.example.jbmsuppliers.sinopec.adapter.Order_XQ_Adapter;
import com.example.jbmsuppliers.sinopec.net.NetAddRess;
import com.example.jbmsuppliers.sinopec.view.DialogLoading;
import com.jbm.jbmsupplier.util.IJOrderCtrl;
import com.jbm.jbmsupplier.util.IorderEntity;
import com.jbm.jbmsupplier.util.Media;
import com.jbm.jbmsupplier.util.OrdAttachmentEntity;

import java.io.InputStream;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

import static com.example.jbmsuppliers.R.id.bu_order_pjdd;
import static com.example.jbmsuppliers.R.id.bu_order_qxdd;

/**
 * Created by 家帮忙 on 2017/2/28.
 */

public class MyOrderDetail extends ActivityUse {


    ArrayList<Bitmap> arrbitmap;
    @InjectView(R.id.layoutxiangqing)
    LinearLayout layoutxiangqing;
    @InjectView(R.id.goback_ps_set)
    Button gobackPsSet;
    @InjectView(R.id.titlelz)
    TextView titlelz;
    @InjectView(R.id.te_order_bainhao)
    TextView teOrderBainhao;
    @InjectView(R.id.te_order_titme)
    TextView teOrderTitme;
    @InjectView(R.id.te_order_name)
    TextView teOrderName;
    @InjectView(R.id.textView3)
    TextView textView3;
    @InjectView(R.id.te_order_photo)
    TextView teOrderPhoto;
    @InjectView(R.id.tr_order_diz)
    TextView trOrderDiz;
    @InjectView(R.id.te_order_neirong)
    TextView teOrderNeirong;
    @InjectView(R.id.gr_order_image)
    GridView grOrderImage;
    @InjectView(R.id.textView5)
    TextView textView5;
    @InjectView(R.id.te_order_wxjg)
    TextView teOrderWxjg;
    @InjectView(R.id.te_order_sgjg)
    TextView teOrderSgjg;

    @InjectView(R.id.linner_order_ddjg)
    LinearLayout linnerOrderDdjg;
    @InjectView(R.id.bu_order_qxdd)
    Button buOrderQxdd;
    @InjectView(R.id.linner_order_dingdan)
    LinearLayout linnerOrderDingdan;
    @InjectView(R.id.bu_order_pjdd)
    Button buOrderPjdd;
    @InjectView(R.id.linner_order_dingdan1)
    LinearLayout linnerOrderDingdan1;
    @InjectView(R.id.te_or_neirong)
    TextView teOrNeirong;

    @InjectView(R.id.te_or_daijinquan)
    TextView teOrDaijinquan;
    private Media m;
    private final int ORDERITEM = 0001;
    private final int ORDERINPUT = 0002;
    private final int BAOXIUDDAN = 0003;
    DialogLoading dialogLoading;
    private SimpleDateFormat fors;
    private NetAddRess netAddRess;

    @Override
    protected int setContentView1() {
        return R.layout.order_main_dingdan_message;
    }

    @Override
    protected void initView() {
        ButterKnife.inject(this);
        titlelz.setText("订单详情");
        arrbitmap = new ArrayList<>();
        initview();
    }


    private void initview() {
        netAddRess = new NetAddRess(handler);
        setshowDate();
    }

    private void setshowDate() {
        dialogLoading = new DialogLoading(this, "加载中...", true);

        netAddRess.getOrderitem(getIntent().getStringExtra("order_id"), ORDERITEM);
        if (dialogLoading != null)
            dialogLoading.show();
    }

    private void setshowOrder(IorderEntity iorder, IJOrderCtrl ijor) {
        if (getIntent().getStringExtra("status").equals("new")) {
            linnerOrderDdjg.setVisibility(View.GONE);
            linnerOrderDingdan1.setVisibility(View.GONE);
            linnerOrderDingdan.setVisibility(View.VISIBLE);
        } else if (getIntent().getStringExtra("status").equals("temp")) {
            linnerOrderDdjg.setVisibility(View.GONE);
            linnerOrderDingdan1.setVisibility(View.GONE);
            linnerOrderDingdan.setVisibility(View.VISIBLE);
        } else if (getIntent().getStringExtra("status").equals("confirm")) {
            linnerOrderDdjg.setVisibility(View.GONE);
            linnerOrderDingdan1.setVisibility(View.GONE);
            linnerOrderDingdan.setVisibility(View.VISIBLE);
        } else if (getIntent().getStringExtra("status").equals("completed")) {
            linnerOrderDingdan.setVisibility(View.GONE);
            teOrderWxjg.setText(iorder.getAmount() + "元");
            teOrderSgjg.setText(iorder.getSellerId());
            teOrNeirong.setText(ijor.IndexofList(0).job_sum);//维修内容
            teOrDaijinquan.setText(iorder.getVoucher()+"元");//代金券
//            te_order_bxq.setText();
        } else if (getIntent().getStringExtra("status").equals("comment")) {
            linnerOrderDingdan.setVisibility(View.GONE);
//            linner_order_dingdan1.setVisibility(Vi);
            buOrderPjdd.setText("完成评价");
            teOrderWxjg.setText(iorder.getAmount() + "元");
            teOrderSgjg.setText(iorder.getSellerId());
            teOrNeirong.setText(ijor.IndexofList(0).job_sum);//维修内容
            teOrDaijinquan.setText(iorder.getVoucher()+"");//代金券
//            te_order_bxq.setText();
        } else if (getIntent().getStringExtra("status").equals("revoke")) {
            linnerOrderDdjg.setVisibility(View.GONE);
            linnerOrderDingdan1.setVisibility(View.GONE);
            linnerOrderDingdan.setVisibility(View.GONE);
        }
    }
//    public void setRevokeorder() {//取消订单
//        if (getIntent().getStringExtra("status").equals("temp")) {
//            NetAddRess netAddRess = new NetAddRess(handler);
//            netAddRess.getOrderCancel(getIntent().getStringExtra("order_id"), getIntent().getStringExtra("status"), ORDERSTATIC);
//            if (dialogLoading != null) {
//                dialogLoading.setTextstr("订单取消中...");
//                dialogLoading.show();
//            }
//
//
//        } else if (getIntent().getStringExtra("status").equals("new")) {
//            NetAddRess netAddRess = new NetAddRess(handler);
//            netAddRess.getOrderCancel(getIntent().getStringExtra("order_id"), getIntent().getStringExtra("status"), ORDERSTATIC);
//            if (dialogLoading != null) {
//                dialogLoading.setTextstr("订单取消中...");
//                dialogLoading.show();
//            }
//        } else if (getIntent().getStringExtra("status").equals("confirm")) {
//            hint("订单正在作业中不能取消");
//        }
//
//
//    }

    private IJOrderCtrl ijor;
    private IorderEntity iorder;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case ORDERITEM: {
                    iorder = (IorderEntity) msg.obj;
                    if (iorder.back) {
                        if (dialogLoading != null) {
                            dialogLoading.dismiss();
                        }
                        teOrderBainhao.setText("订单编号:" + "  " + getIntent().getStringExtra("order_id"));
                        if (iorder.getCreateDate() != null) {
                            Timestamp times = iorder.getCreateDate();

                            teOrderTitme.setText(times.toString().substring(0,times.toString().indexOf("."))+"");
                        }//时间
                        if (iorder.getItemDesc() != null) {
                            teOrderNeirong.setText(iorder.getItemDesc());
                        }//内容

                        teOrderName.setText(iorder.getContacts());//姓名
                        teOrderPhoto.setText(iorder.getMobile());//手机号
                        trOrderDiz.setText("   " + iorder.getGiText());//地址
                        //获取报修内容地址

                        netAddRess.getBaoXiuXinxi(getIntent().getStringExtra("order_id"), BAOXIUDDAN);
                    } else if (iorder.equals("time out")) {
                        if (dialogLoading != null) {
                            dialogLoading.dismiss();
                        }
                        hint(iorder.errorinfo);
                        return;
                    } else {
                        if (dialogLoading != null) {
                            dialogLoading.dismiss();
                        }
                    }

                    break;
                }
                case ORDERINPUT: {
                    InputStream inputStream = (InputStream) msg.obj;
                    Bitmap inputstrea = BitmapFactory.decodeStream(inputStream);


                    arrbitmap.add(inputstrea);
                    if (arrbitmap.size() > -1) {
                        grOrderImage.setVisibility(View.VISIBLE);
                    }

                    grOrderImage.setAdapter(new Order_XQ_Adapter(MyOrderDetail.this, arrbitmap));

                    break;
                }
                case BAOXIUDDAN: {
                    ijor = (IJOrderCtrl) msg.obj;
                    if (ijor.back) {
                        setshowOrder(iorder, ijor);
                        if (iorder.getOrdattachmentlist() != null) {
                            for (int j = 0; j < iorder.getOrdattachmentlist().size(); j++) {
                                m = new Media();
                                OrdAttachmentEntity me = iorder.getOrdattachmentlist().get(j);
                                m.mid = me.getOrdAttId();
                                m.mtype = me.getAttType();
                                m.filename = me.getAttOrgname();
                                m.filepath = me.getAttPath();
                            }

                            netAddRess.getOrderinput(getIntent().getStringExtra("order_id"), m, ORDERINPUT);
                        } else {
                            return;
                        }
                    } else if (ijor.equals("time out")) {
                        return;
                    } else {
                        return;
                    }

                }
                super.handleMessage(msg);
            }
        }
    };


    @OnClick({bu_order_qxdd, bu_order_pjdd})
    public void onClick(View view) {
        switch (view.getId()) {
            case bu_order_qxdd:
//                if (setConnectMana().getActiveNetworkInfo() != null) {
//                setRevokeorder();
//                } else {
//                    hint("网络无连接，请稍后再试！");
//
//                }
                break;
            case bu_order_pjdd:
////                if (setConnectMana().getActiveNetworkInfo() != null) {
//                if (!getIntent().getStringExtra("status").equals("comment")) {
//                    startActivity(new Intent(MyOrderDetail.this, MyOrderEvalua.class).putExtra("id", getIntent().getStringExtra("order_id")));
//                } else {
//                    hint("订单已完成评价,不能再次评价!");
//                }
//                } else {
//                    hint("网络无连接，请稍后再试!");
//
//                }
                break;
        }
    }


    @OnClick(R.id.goback_ps_set)
    public void onClick() {
        finish();
    }

}
